import {defineStore} from "pinia"
import store from "store2";
import _ from "lodash"


const useWebsiteStore = new defineStore('websiteStore', {
    state: () => ({
        websiteList: [],
        keywords: "",
    }),
    getters: {
        queryWebsite() {
            return (keywords) => {
                if (!keywords || keywords === "") {
                    return this.websiteList
                } else {
                    let websiteList = _.filter(this.websiteList, function (value) {
                        return value.title.toLowerCase().indexOf(keywords) > -1
                    })
                    return websiteList
                }
            }
        }
    },
    actions: {
        setKeywords(value) {
            this.keywords = value;
        },
        setWebsiteList(item) {
            this.websiteList.unshift(item);
            store.set("website", this.websiteList)
        },
        getWebsiteList() {
            this.websiteList = store.get("website") || [];
        },
        deleteWebsite(item) {
            let index = _.findIndex(this.websiteList, {url: item.url});
            this.websiteList.splice(index, 1);
            store.set("website", this.websiteList);
        }
    },
})

export default useWebsiteStore
